<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="（2017.8.14） URLurl: scheme:://host:port/path?query#fragment  scheme: 协议 http/https/ftp/file host: 域名或者IP地址 path: 路径 port: 端口(文件配置时1024以下的端口最好不要用)，区分网络应用程序，相同的应用程序之间交互信息 query: 查询字符串。参数格式: key=value&amp;am">
<meta property="og:type" content="article">
<meta property="og:title" content="Ajax跨域">
<meta property="og:url" content="http://yoursite.com/2017/09/21/ajax跨域/index.html">
<meta property="og:site_name" content="西瓜古古🍉">
<meta property="og:description" content="（2017.8.14） URLurl: scheme:://host:port/path?query#fragment  scheme: 协议 http/https/ftp/file host: 域名或者IP地址 path: 路径 port: 端口(文件配置时1024以下的端口最好不要用)，区分网络应用程序，相同的应用程序之间交互信息 query: 查询字符串。参数格式: key=value&amp;am">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-21T10:22:38.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Ajax跨域">
<meta name="twitter:description" content="（2017.8.14） URLurl: scheme:://host:port/path?query#fragment  scheme: 协议 http/https/ftp/file host: 域名或者IP地址 path: 路径 port: 端口(文件配置时1024以下的端口最好不要用)，区分网络应用程序，相同的应用程序之间交互信息 query: 查询字符串。参数格式: key=value&amp;am">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>

  <title> Ajax跨域 | 西瓜古古🍉 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">西瓜古古🍉</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Ajax跨域
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2017-09-21T18:22:38+08:00" content="2017-09-21">
              2017-09-21
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/Ajax/" itemprop="url" rel="index">
                    <span itemprop="name">Ajax</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>（2017.8.14）</p>
<h2 id="URL"><a href="#URL" class="headerlink" title="URL"></a>URL</h2><p>url: scheme:://host:port/path?query#fragment</p>
<ul>
<li>scheme: 协议 http/https/ftp/file</li>
<li>host: 域名或者IP地址</li>
<li>path: 路径</li>
<li>port: 端口(文件配置时1024以下的端口最好不要用)，区分网络应用程序，相同的应用程序之间交互信息</li>
<li>query: 查询字符串。参数格式: key=value&amp;key2=value2</li>
<li>fragment: 锚点、hash哈希，定位页面的额某个位置<h2 id="同源"><a href="#同源" class="headerlink" title="同源"></a>同源</h2>协议，域名，端口均相同</li>
</ul>
<h2 id="跨域"><a href="#跨域" class="headerlink" title="跨域"></a>跨域</h2><p>不满足同源即为跨域<br>具备跨域资源请求能力的标签：iframe、link、script、video、audeo</p>
<h2 id="解决-方法"><a href="#解决-方法" class="headerlink" title="解决 方法"></a>解决 方法</h2><h3 id="解决方法-cors（Cross-origin-resource-sharing）"><a href="#解决方法-cors（Cross-origin-resource-sharing）" class="headerlink" title="解决方法 cors（Cross-origin resource sharing）"></a>解决方法 cors（Cross-origin resource sharing）</h3><ul>
<li>cors在移动端支持度不错，可以考虑在移动端全面尝试</li>
<li>pc上有不兼容和没有完美支持，小心踩坑</li>
<li>jsop是get形式，承载的信息量有限，所以信息量较大时cors是不错的选择</li>
</ul>
<p>&lt;?php<br>    header(‘Access-Control-Allow-Origin:<a href="http://www.study.com" target="_blank" rel="external">http://www.study.com</a>‘);<br>    -&gt; 允许固定网址可访问<br>    header(‘Access-Control-Allow-Origin:*’);<br>    -&gt; 允许所有访问<br>?&gt;</p>
<h3 id="解决方法-jsonp"><a href="#解决方法-jsonp" class="headerlink" title="解决方法 jsonp"></a>解决方法 jsonp</h3><ul>
<li>只适合get请求</li>
<li>jsonp利用了script标签的可跨域特性 </li>
<li><p>每发一次请求就创建一个Script标签，在标签中传输信息</p>
</li>
<li><p>调用<br>$.ajax({<br> url: ‘url—‘,<br> jsonp: “”,  // 约定等号左边的值key(服务器根据该名称获取回调函数 )<br> jsonpCallback:  // 约定等号右边的value<br>})</p>
<h3 id="反向代理"><a href="#反向代理" class="headerlink" title="反向代理"></a>反向代理</h3></li>
<li>修改配置文件<br>将想要访问的网址加入到配置该虚拟域名文档中</li>
<li>示例<br>例如：当前是www.study.com中的文件，想要访问api.study.com<br>配置虚拟域名的文档httpd-vhosts.conf中<br>在www.study.com的配置行加入ProxyPass /api <a href="http://api.study.com" target="_blank" rel="external">http://api.study.com</a><br>ajax请求是url必须写成/api<h3 id="解决方法-document-domain-iframe标签"><a href="#解决方法-document-domain-iframe标签" class="headerlink" title="解决方法 document.domain + iframe标签"></a>解决方法 document.domain + iframe标签</h3></li>
<li><p>适合顶级域名相同的情况<br>如 www.baidu.com 和 api.baidu.com</p>
</li>
<li><p>互相访问的两个html文件都设置</p>
<script>
  document.domain = "baidu.com";
</script>

</li>
</ul>
<h3 id="解决方法-hash"><a href="#解决方法-hash" class="headerlink" title="解决方法  hash"></a>解决方法  hash</h3><h3 id="解决方法-name"><a href="#解决方法-name" class="headerlink" title="解决方法  name"></a>解决方法  name</h3>
      
    </div>

    <div>
      
        
      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/21/模块化规范/" rel="next" title="模块化规范">
                <i class="fa fa-chevron-left"></i> 模块化规范
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/22/jQuery插件--uploadify/" rel="prev" title="jQuery插件--uploadify">
                jQuery插件--uploadify <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/23103K3MK.jpg"
               alt="beikexixi" />
          <p class="site-author-name" itemprop="name">beikexixi</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">35</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">9</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">10</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com" target="_blank" title="Github">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  Github
                </a>
              </span>
            
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#URL"><span class="nav-number">1.</span> <span class="nav-text">URL</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#同源"><span class="nav-number">2.</span> <span class="nav-text">同源</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#跨域"><span class="nav-number">3.</span> <span class="nav-text">跨域</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#解决-方法"><span class="nav-number">4.</span> <span class="nav-text">解决 方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#解决方法-cors（Cross-origin-resource-sharing）"><span class="nav-number">4.1.</span> <span class="nav-text">解决方法 cors（Cross-origin resource sharing）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#解决方法-jsonp"><span class="nav-number">4.2.</span> <span class="nav-text">解决方法 jsonp</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#反向代理"><span class="nav-number">4.3.</span> <span class="nav-text">反向代理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#解决方法-document-domain-iframe标签"><span class="nav-number">4.4.</span> <span class="nav-text">解决方法 document.domain + iframe标签</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#解决方法-hash"><span class="nav-number">4.5.</span> <span class="nav-text">解决方法  hash</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#解决方法-name"><span class="nav-number">4.6.</span> <span class="nav-text">解决方法  name</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">beikexixi</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.1"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  



  



  
  
  

  

  

</body>
</html>
